<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="88px"
    >
      <el-form-item label="Tag Name" prop="noticeTitle">
        <el-input
          v-model="queryParams.context"
          placeholder="Add new tag name here"
          clearable
          size="small"
          closable
        />
      </el-form-item>
      <el-form-item class="btns">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >Add</el-button
        >
        <el-button
          type="warning"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >Search</el-button
        >
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          @click="delAllSelection"
          >Delete</el-button
        >
      </el-form-item>
    </el-form>
    <el-table :data="labelList" class="tableBox" @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"
        align="center"
      ></el-table-column>
      <el-table-column label="No." type="index" align="center" width="200" />
      <el-table-column label="Tag" align="center" prop="context" />
      <el-table-column
        label="Operation"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            >Edit</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="Edit Tag" :visible.sync="labelVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="Tag Name" label-width="120px" prop="context">
          <el-input v-model="form.context"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">Cancel</el-button>
        <el-button type="primary" @click="submitForm('form')">Confirm</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { labelList, addLabel, updateLabel, delLabel } from "@/api/system/label";
export default {
  name: "Label",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 标签表格数据
      labelList: [],
      // 多选框数据
      multipleSelection: [],
      delList:[], 
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        context: null,
      },

      // 控制标签显示与隐藏
      labelVisible: false,
      form: {},
      rules: {
        context: [{ required: true, message: "Add new tag name here", trigger: "blur" }],
      },
    };
  },
  filters: {
    formatDate: function (value) {
      let date = new Date(parseInt(value) * 1000);
      let y = date.getFullYear();
      let m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      let h = date.getHours();
      h = h < 10 ? "0" + h : h;
      let minute = date.getMinutes();
      let second = date.getSeconds();
      minute = minute < 10 ? "0" + minute : minute;
      second = second < 10 ? "0" + second : second;
      return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
    },
  },
  created() {
    this.getList();
  },
  methods: {
    // 删除操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(val);
    },
    /** 查询列表 */
    getList() {
      labelList(this.queryParams)
        .then((res) => {
          if (res.code == 200) {
            this.labelList = res.rows;
            this.total = res.total;
          }
        })
        .catch(() => {
          console.log("error submit!!");
          return false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /* 点击添加 */
    handleAdd() {
      if (this.queryParams.context == null) {
        return this.msgError("Add tag cannot be empty!");
      }
      addLabel({
        context: this.queryParams.context,
      }).then((res) => {
        console.log(res);
        if (res.code == 200) {
          this.getList();
          this.msgSuccess("Add tag success!");
          this.queryParams.context = "";
        }
      });
    },

    /** 点击编辑 */
    handleEdit(row) {
      this.labelVisible = true;
      this.form = row;
    },

    /** 提交编辑内容 */
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateLabel(this.form).then((res) => {
            if (res.code == 200) {
              this.labelVisible = false;
              this.getList();
              this.form = {};
              this.msgSuccess("Modify tag successfully!");
            } else {
              this.labelVisible = false;
              this.form = {};
              this.msgError("Fail to edit");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    /** 取消编辑 */
    cancel() {
      this.labelVisible = false;
      this.form = {};
      this.getList();
    },
    /** 删除按钮操作 */
    deleteData(id) {
      delLabel(id)
        .then((res) => {
          if (res.code == "200") {
            this.$message.success("Delete successful");
            this.getList();
          } else {
            this.$message.error("Delete failed");
          }
        })
    },
    delAllSelection() {
      const length = this.multipleSelection.length;
      let str = "";
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].id + ",";
      }
      if (str.length == 0) {
        this.$message.error("Must choose one");
        return;
      }
      this.$confirm("Confirm to delete?", "Tips", {
        type: "warning",
      })
        .then(() => {
          this.deleteData(str);
          this.multipleSelection = [];
        })
        .catch(() => {});
    }
  },
};
</script>
<style lang="scss" scoped>
textarea {
  // width: 95%;
  box-sizing: border-box;
  padding: 5px;
  color: #666;
  resize: vertical;
}
.info {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.btns {
  margin-left: 20px;
}
.tableBox {
  margin-top: 15px;
}
</style>
